<?php 
//Config : Les informations personnels de l'instance (log, pass, etc)
require("../../include/config.php");

//API Fonctions : les fonctions fournis de base par l'API
require("../../API/php/fonctions.php");

//Header établie la connection à la base $connection
require("../../API/php/header.php");

//Mode debug
$modeDebug = false;

//Public ou privé (clé obligatoire)
$modePublic = true;

//Mode de sortie text,json,xml,csv
//pour xml et csv $object_retour->data["resultat"] doit contenir qu'un est unique array
$modeSortie = "json";

//Liens de test
// API_RH/phpsql/mysql_getSemaines.php?milis=123456789&ctrl=ok

// IN obligatoire
$arrayInput = array(
    "ctrl" => null
);

//Récupération des entrants
$arrayValeur = recupInput($arrayInput);

//Object retour minima
// $object_retour->strErreur string
// $object_retour->data  string
// $object_retour->statut  string

//------------------------------------------------------------------
//pourcent de saisi d activitee
$strSql = "SELECT b.lundi as lundi_sql, DATE_FORMAT(b.lundi,'%d/%m/%Y') as lundi_fr, b.dimanche as dimanche_sql, DATE_FORMAT(b.dimanche,'%d/%m/%Y') as dimanche_fr
FROM (
    SELECT a.L as lundi, DATE_ADD(a.L, INTERVAL +6 DAY) as dimanche
    FROM (
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*0 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*1 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*2 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*3 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*4 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*5 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*6 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*7 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*8 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*9 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*10 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*11 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*12 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*13 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*14 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*15 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*16 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*17 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*18 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*19 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*20 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*21 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*22 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*23 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*24 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*25 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*26 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*27 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*28 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*29 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*30 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*31 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*32 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*33 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*34 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*35 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*36 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*37 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*38 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*39 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*40 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*41 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*42 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*43 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*44 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*45 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*46 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*47 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*48 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*49 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*50 DAY) as L
        UNION ALL
        SELECT DATE_ADD(	DATE_ADD(CURDATE(), INTERVAL (5 + DAYOFWEEK(CURDATE())) * -1 DAY), INTERVAL -7*51 DAY) as L
    ) a
) b	
;";
$req = $connection->prepare($strSql);

if($req->execute()){
    // On indique que nous utiliserons les résultats en tant qu'objet
    $req->setFetchMode(PDO::FETCH_OBJ);

    // On transforme les résultats en tableaux d'objet
    $resultats = new stdClass();
    $resultats->data = $req->fetchAll(PDO::FETCH_OBJ);
    $resultats->nombre = count($resultats->data);

    $object_retour->data["resultat"] = $resultats;
}else{
    $error = 'Erreur SQL:'.print_r($req->errorInfo(), true)." (".$strSql.")";
    $object_retour->strErreur = $error;
}
$req->closeCursor();
//--------------------------------------------------------------------------

require("../../API/php/footer.php");
?>